#include"common.hpp"

int main()
{
  umask(0);
  int n = mkfifo(str.c_str(),m);//创建命名管道
  if(n < 0)
  {
    cout << "creat fifo fail " << errno << " " << strerror(errno) << endl;
    return -1;
  }
  cout << "success creat fifo" << endl;
  
  int fd = open(str.c_str(),O_RDONLY);//打开命名管道
  if(fd < 0)
  {
    cout << "open file fail " << errno << strerror(errno) << endl;
    return -1;
  }
  cout << "success open file" << endl;
  char buffer[1024];
  while(true)
  {
    cout << "我是服务端，正在接收来自客户端的消息" << endl;
    ssize_t writen = read(fd,buffer,sizeof(buffer) - 1);
    if(writen > 0)
    {
      buffer[writen] = '\0';
      cout << "我是服务端，接收到的信息是: " << buffer << endl;
    }
    else if(writen == 0)
    {
      cout << "客户端要我退出了" << endl;
      break;
    }
    else
    {
      cout << "服务端在读取的过程中，发生错误 : " << errno << strerror(errno) << endl;
      break;
    }
  }
  close(fd);
  unlink(str.c_str());//这样就不用在外面手动删除fifo文件了
  return 0;
}


